﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;

namespace Hzd.RegexHelper
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        #region 开始任务
        private void Start()
        {
            label4.Text = "正则匹配的结果";
            this.richTxt_Result.Text = null;
            string[] strArray = null;
            this.Btn_Start.Text = "正在测试";
            this.Btn_Start.Enabled = false;
            if (this.checkBox1.Checked)
            {
                lbl_Stats.Text = "正在下载网页代码...";
                this.richTxt_Code.Text = Helper.GetHtml(this.txt_Url.Text, true);

                //这里了
                if (this.txt_Regtest.Text.Length == 0)
                {
                    this.Btn_Start.Text = "开始测试";
                    this.Btn_Start.Enabled = true;
                    lbl_Stats.Text = "测试结束，未匹配到结果这个";
                    return;
                }
                strArray = Helper.getFinalResult(Helper.zregex(this.richTxt_Code.Text, this.txt_Regtest.Text));
                if (strArray.Length > 0)
                {
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        if (i != (strArray.Length - 1))
                        {
                            string text = this.richTxt_Result.Text;
                            this.richTxt_Result.Text = text + "匹配结果" + Convert.ToString((int)(i + 1)) + ":" + strArray[i] + "\r\n";

                        }
                        else
                        {
                            string str2 = this.richTxt_Result.Text;
                            this.richTxt_Result.Text = str2 + "匹配结果" + Convert.ToString((int)(i + 1)) + ":" + strArray[i];

                        }
                    }
                    lbl_Stats.Text = "匹配到" + Convert.ToString(strArray.Length) + "个结果";
                }
            }
            else
            {
                if ((this.txt_Regtest.Text.Length == 0) || (this.richTxt_Code.Text.Length == 0))
                {
                    this.Btn_Start.Text = "开始测试";
                    this.Btn_Start.Enabled = true;
                    lbl_Stats.Text = "测试结束，未匹配到结果";
                    return;
                }
                strArray = Helper.getFinalResult(Helper.zregex(this.richTxt_Code.Text, this.txt_Regtest.Text));
                if (strArray.Length > 0)
                {
                    for (int j = 0; j < strArray.Length; j++)
                    {
                        if (j != (strArray.Length - 1))
                        {
                            this.richTxt_Result.Text = this.richTxt_Result.Text + strArray[j] + "\r\n";
                        }
                        else
                        {
                            this.richTxt_Result.Text = this.richTxt_Result.Text + strArray[j];
                        }
                    }
                    lbl_Stats.Text = "匹配到" + Convert.ToString(strArray.Length) + "个结果";
                }
            }
            this.Btn_Start.Text = "开始测试";
            this.Btn_Start.Enabled = true;
        } 
        #endregion

        private void Btn_Start_Click(object sender, EventArgs e)
        {
            new Thread(new ThreadStart(Start)).Start();
        }

        private void richTxt_Code_TextChanged(object sender, EventArgs e)
        {
            this.richTxt_Code.SelectAll();
        }

        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            label4.Text = "正则描述";
            richTxt_Result.Text = "";
            richTxt_Result.Text = listView1.Items[listView1.SelectedIndices[0]].SubItems[0].Text + "\r\n" + "-----------------------------------------------------------------------------------------------" + "\r\n" + listView1.Items[listView1.SelectedIndices[0]].SubItems[1].Text;
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                try
                {
                    //MessageBox.Show(treeView1.SelectedNode.Text);
                    label4.Text = "常用的正则以及描述";
                    richTxt_Result.Text = "";
                    string str = this.treeView1.SelectedNode.Tag.ToString();
                    txt_Regtest.Text = str.Substring(0, str.IndexOf("%%"));
                    richTxt_Result.Text = this.treeView1.SelectedNode.Text + "\r\n" + "-----------------------------------------------------------------------------------------------" + "\r\n" + txt_Regtest.Text + "\r\n" + str.Substring(str.IndexOf("%%") + 2, str.Length - str.IndexOf("%%") - 2);

                }
                catch
                {

                }
            }
        }
    }
}
